package com.module.shici.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;


import com.module.shici.R;
import com.module.shici.bean.StoryBean;
import com.module.shici.room.Music;

import java.util.List;

public class StoryRvAdapter extends RecyclerView.Adapter<StoryRvAdapter.ViewHolder> {

    private String[][] titleArray;
    private int[] imgArray;
    private List<Music> musicList;
    private int imgType;

    public StoryRvAdapter(String[][] titleArray, int[] imgArray) {
        this.titleArray = titleArray;
        this.imgArray = imgArray;
    }

    public StoryRvAdapter(List<Music> musicList, int imgType) {
        this.musicList = musicList;
        this.imgType = imgType;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.module_shici_rv_story_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (musicList == null){
            holder.title.setText(titleArray[position][0]);
            holder.relativeLayout.setBackgroundResource(imgArray[position%imgArray.length]);

            holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (storyListener != null){
                        storyListener.setStoryUrl(titleArray[position][1],position);
                    }
                }
            });
        }else {
            holder.title.setText(musicList.get(position).getChaper());
            switch (imgType){
                case 1:
                    holder.relativeLayout.setBackgroundResource(musicList.get(position).getStatus());
                    break;
                case 2:
                    holder.relativeLayout.setBackgroundResource(StoryBean.storyGSImg[position%4]);
                    break;
                case 3:
                    holder.relativeLayout.setBackgroundResource(StoryBean.storyEGImg[position%4]);
                    break;
                case 4:
                    holder.relativeLayout.setBackgroundResource(StoryBean.storyTHImg[position%4]);
                    break;
            }


            holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (storyListener != null){
                        storyListener.setStoryUrl(null,position);
                    }
                }
            });
        }

    }

    public interface StoryListener{
        void setStoryUrl(@Nullable String url,@Nullable int position);
    }

    private StoryListener storyListener;

    public void getStoryUrl(StoryListener storyListener){
        this.storyListener = storyListener;
    }

    @Override
    public int getItemCount() {
        return musicList == null ? titleArray.length : musicList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout relativeLayout;
        private ImageView img;
        private TextView title;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            relativeLayout = itemView.findViewById(R.id.rv_story_item_rl);
            img = itemView.findViewById(R.id.rv_story_item_img);
            title = itemView.findViewById(R.id.rv_story_item_title);
        }
    }
}
